package com.kaibes.admin.message;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.module.message.MessageService;
import com.kaibes.web.admin.AdminController;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.thymeleaf.ThymeleafConfig;
import com.kaibes.web.thymeleaf.Variable;

@AdminController
@RequestMapping("/admin/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private ThymeleafConfig thymeleafConfig;
    private List<Variable> variables;

    @GetMapping("/send")
    @ApiLink("发送消息")
    public String send(Model model, @RequestParam(required = false) Long toId) {
        if (variables == null) {
            variables = new ArrayList<>(thymeleafConfig.getBaseVariables());
        }
        model.addAttribute("variables", variables);
        model.addAttribute("toId", toId);
        return "message-send";
    }

    @GetMapping("/list")
    @ApiLink("消息列表")
    public String list(Model model, @RequestParam(defaultValue = "25") int size,
            @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "") String search) {
        search = search.trim();
        if (!search.isEmpty()) {
            model.addAttribute("messagePage",
                    messageService.pageByTitleLikeOrContentLike(page, size, search));
        } else {
            model.addAttribute("messagePage", messageService.page(page, size));
        }
        return "message-list";
    }

}
